package com.example.shopping.controller;

import com.example.shopping.pojo.Commodity;
import com.example.shopping.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@Controller
@CrossOrigin
public class CommodityController {
    @Autowired(required = false)
    private CommodityService commodityService;

    @RequestMapping("/selectCommodityList")
    public String selectCommodityList(HttpServletRequest request) {
        List<Commodity> commodities = commodityService.selectCommodityList();
        request.getSession().setAttribute("commodities", commodities);
        return "commodity";
    }

    @RequestMapping("/selectCommodityByLikeName")
    public String selectCommodityByLikeName(HttpServletRequest request, String name) {
        List<Commodity> commodities = commodityService.selectCommodityByLikeName(name);
        request.getSession().setAttribute("commodities", commodities);
        return "commodity";
    }

    @RequestMapping("/selectCommodityByStatus")
    public String selectCommodityByStatus(HttpServletRequest request, int status) {
        List<Commodity> commodities = commodityService.selectCommodityByStatus(status);
        request.getSession().setAttribute("commodities", commodities);
        return "commodity";
    }

    @RequestMapping("/deleteCommodityById")
    public String deleteCommodityById(int id) {
        int i = commodityService.deleteCommodityById(id);
        if (i > 0) {
            return "redirect:selectCommodityList";
        }
        return "redirect:deleteCommodityById";
    }
}

